<template>
  <a-modal
    title="客户九宫格管理"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="图片名称">
          <a-input placeholder="图片别名" v-decorator="[ 'name', {rules: [{required: true, message: '请选择。。。' }] }]"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="跳转地址">
          <a-input placeholder="跳转地址" v-decorator="[ 'transUrl', {rules: [{required: true, message: '请选择。。。' }] }]"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序">
          <a-input placeholder="排序" v-decorator="[ 'sortNo', {rules: [] }]"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="图片">
          <a-upload
            listType="picture-card"
            class="avatar-uploader"
            :showUploadList="false"
            :action="baseUrl + '/sys/upload/uploadFile'"
            :beforeUpload="beforeUpload"
            @change="handleChange"
          >
            <img v-if="imageUrl" :src="imageUrl" alt="avatar" width="300px"/>
            <div v-else>
              <a-icon :type="confirmLoading ? 'loading' : 'plus'" />
              <div class="ant-upload-text">选择图片</div>
            </div>
          </a-upload>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { saveUploadFile } from '@/api/system/uploadFile'

  export default {
    data () {
      return {
        baseUrl: process.env.VUE_APP_API_BASE_URL,
        mdl:{},
        masterId: null,
        id: null,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        imageUrl: null,
        fileName: null,

        form: this.$form.createForm(this)
      }
    },
    mounted () {

    },
    methods: {
      add (masterId) {
        this.visible = true
        this.masterId = masterId
      },
      edit (record) {
        this.visible = true
        this.id = record.id
        this.imageUrl = record.fileUrl;
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          const formData = pick(record, ['name', 'transUrl', 'sortNo'])
          setFieldsValue(formData)
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            values.filename = this.filename
            values.masterId = this.masterId
            values.masterType = 5
            saveUploadFile(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.visible = false
        this.form.resetFields()
        this.id = null
        this.imageUrl = null
        this.filename = null
      },
      handleChange(info) {
        if (info.file.status === 'uploading') {
          this.confirmLoading = true;
          return;
        }
        if (info.file.status === 'done') {
          // Get this url from response in real world.
          this.imageUrl = info.file.response.fileUrl;
          this.filename = info.file.response.filename
          this.confirmLoading = false;
        }
      },
      beforeUpload(file) {
        // const isJPG = file.type === 'image/jpeg';
        // if (!isJPG) {
        //   this.$message.error('You can only upload JPG file!');
        // }
        // const isLt2M = file.size / 1024 / 1024 < 2;
        // if (!isLt2M) {
        //   this.$message.error('Image must smaller than 2MB!');
        // }
        return true;
      },
    },
  }
</script>
